/* Copyright (c) 2009, Antonie Jovanoski
 *	
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contact e-mail: Antonie Jovanoski <minimoog77@gmail.com>
 */

#include "texteditwithlimit.h"

TextEditWithLimit::TextEditWithLimit(QWidget *parent)
	:	QPlainTextEdit(parent), m_limit(16777215), m_isOverLimit(false)
{
	connect(this, SIGNAL(textChanged()), SLOT(onTextChanged()));
}

void TextEditWithLimit::setLimit(int limit)
{
	m_limit = limit;
}

int TextEditWithLimit::limit() const
{
	return m_limit;
}

void TextEditWithLimit::onTextChanged()
{
	if(toPlainText().size() > m_limit){

		if(!m_isOverLimit){
			m_isOverLimit = true;
			emit overLimit(true);
			setStyleSheet("QPlainTextEdit { background-color: red }");
		}
		
	} else {

		if(m_isOverLimit){
			m_isOverLimit = false;
			emit overLimit(false);
			//Be careful TODO: what if style sheet is set in QDesigner? append? 
			setStyleSheet(QString());
		}
	}
}

void TextEditWithLimit::keyPressEvent(QKeyEvent *e)
{
	//ignore Enter and Tab
	switch(e->key()){
		case Qt::Key_Tab:
			break;
		case Qt::Key_Return:
			emit returnPressed();
			break;
		case Qt::Key_Enter:
			emit returnPressed();
			break;

		default:
			QPlainTextEdit::keyPressEvent(e);
	}
}
